package load;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Calendar;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class LoadTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		if (args.length < 3) {
			System.out.println("Error, Missing parameters.");
			return;
		}

		String host;
		int tcpPort = 0;
		String analyticsBndName;

		host = args[0];
		try {
			tcpPort = Integer.parseInt(args[1]);
		} catch (NumberFormatException e) {
			System.out.println("Error, invalid port number.");
			return;
		}
		analyticsBndName = args[2];

		int clientCnt;
		int auctionsPerMin;
		int auctionDuration;
		int updateIntervalSec;
		int bidsPerMin;
		long start = Calendar.getInstance().getTimeInMillis();

		InputStream is = ClassLoader
				.getSystemResourceAsStream("loadtest.properties");
		if (is != null) {
			Properties props = new java.util.Properties();
			try {
				props.load(is);
				clientCnt = Integer.parseInt(props.getProperty("clients"));
				auctionsPerMin = Integer.parseInt(props
						.getProperty("auctionsPerMin"));
				auctionDuration = Integer.parseInt(props
						.getProperty("auctionDuration"));
				updateIntervalSec = Integer.parseInt(props
						.getProperty("updateIntervalSec"));
				bidsPerMin = Integer.parseInt(props.getProperty("bidsPerMin"));

				ExecutorService es = Executors.newCachedThreadPool();

				for (int i = 0; i < clientCnt; i++) {
					es.execute(new LoadTestThread(i, host, tcpPort, start,
							auctionsPerMin, auctionDuration, updateIntervalSec,
							bidsPerMin));
				}

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} else {
			System.err.println("Properties file not found!");
		}

	}

}
